package net.scrumtool.controller;

import net.scrumtool.model.Collaborator;
import net.scrumtool.technics.DAO;
import net.scrumtool.technics.Utils;
import net.scrumtool.view.Connexion;
import net.scrumtool.view.Home;

import org.eclipse.jface.text.TextUtilities;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

public class Cconnexion {
	Connexion view;

	public Cconnexion(Connexion view) {
		this.view = view;
	}

	public void init() {
		view.getBtnConnexion().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent evt) {

				if(!view.getTxtLogin().getText().equals("") || view.getTxtPassword().equals("")){
					if (DAO.isExistUser(view.getTxtLogin().getText(), view.getTxtPassword().getText())) {
						Home home = new Home();
						Collaborator userOnline = DAO.getInfosUser(view.getTxtLogin().getText(), view.getTxtPassword().getText());
	
						view.close();
						home.init();
	
						MainController mc = new MainController(home);
						MainController.userActif = userOnline;
						mc.init(home);
						home.open();
					} else {
						Utils.MsgBox(view.getShell(), "Error, incorrect login","information");
					}
				}else{
					Utils.MsgBox(view.getShell(), "Error, incorrect login","information");
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getTxtPassword().addKeyListener(new KeyListener() {

			@Override
			public void keyReleased(KeyEvent event) {
				if (event.keyCode == 13) {
					if (DAO.isExistUser(view.getTxtLogin().getText(), view.getTxtPassword().getText())) {
						Home home = new Home();
						Collaborator userOnline = DAO.getInfosUser(view.getTxtLogin().getText(), view.getTxtPassword().getText());

						view.close();
						home.init();

						MainController mc = new MainController(home);
						MainController.userActif = userOnline;
						mc.init(home);
						home.open();
					} else {
						Utils.MsgBox(view.getShell(), "Error, incorrect login","information");
					}
				}

			}

			@Override
			public void keyPressed(KeyEvent event) {
				// TODO Auto-generated method stub

			}
		});

		view.getTxtLogin().addKeyListener(new KeyListener() {

			@Override
			public void keyReleased(KeyEvent event) {
				if (event.keyCode == 13) {
					if (DAO.isExistUser(view.getTxtLogin().getText(), view.getTxtPassword().getText())) {
						Home home = new Home();
						Collaborator userOnline = DAO.getInfosUser(view.getTxtLogin().getText(), view.getTxtPassword().getText());

						view.close();
						home.init();
	
						MainController mc = new MainController(home);
						MainController.userActif = userOnline;
						mc.init(home);
						home.open();
					} else {
						Utils.MsgBox(view.getShell(), "Error, incorrect login","information");
					}
				}

			}

			@Override
			public void keyPressed(KeyEvent event) {
				// TODO Auto-generated method stub

			}
		});

	}
}
